#Twisted Shootout
#Copyright (C) 2007 Han Dao
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

#You can contact the author at wikipediankiba@gmail.com
import pygame
import data
import os
import sys
import mapengine
import editnav
import editcollide
import select
import mapeditchanger
import startdisplay


class MapEdit:
	def __init__(self,name):
		self.data = data.read
		self.data.name = name
		self.mapsprites = pygame.sprite.RenderPlain()
		self.engine = mapengine.MapEngine(self.mapsprites,name)
		self.nav = editnav.EditNav()
		self.sel = select.Select()
		self.nav.activate()
		self.sel.activate()
		self.changer = mapeditchanger.MapEditChanger(self.engine,self.sel)
		self.collide = editcollide.EditCollide(self.nav,self.sel)
		self.startdisplay = startdisplay.StartDisplay(self.engine,self.sel)
		self.startdisplay.roll()
		self.changer.old = self.startdisplay.begin
	def run(self,display):
		self.cloud = data.read.load('background',True)
		self.toolbox = data.read.load("toolbox",True)
		self.menu = data.read.load("menu",True)
		while 1:
			pygame.display.flip()
			for k in pygame.event.get():
				if k.type == pygame.QUIT:
					return
				if k.type == pygame.KEYDOWN:
					if k.key == pygame.K_ESCAPE:
						return
					elif k.key == pygame.K_UP:
						self.engine.position.moveup()
					elif k.key == pygame.K_DOWN:
						self.engine.position.movedown()
					elif k.key == pygame.K_LEFT:
						self.engine.position.moveleft()
					elif k.key == pygame.K_RIGHT:
						self.engine.position.moveright()
					elif k.key == pygame.K_c:
						self.sel.value = 0
						self.sel.change()
					elif k.key == pygame.K_s:
						self.data.mapwrite(self.engine.obj)
				if k.type == pygame.MOUSEBUTTONDOWN:
					pos = pygame.mouse.get_pos()
					key = pygame.Rect(pos[0],pos[1],1,1)
					self.move(key)
					self.choose(key)
					self.change(key)
			self.draw(display)
	def changemap(self,name):
		self.data.name = name
	def draw(self,display):
		display.blit(self.cloud,[0,0])
		self.mapsprites.draw(display)
		display.blit(self.toolbox,[800,0])
		display.blit(self.menu,[0,600])
		self.nav.sprites.draw(display)
		self.sel.sprites.draw(display)
		self.engine.items.sprites.draw(display)
	def move(self,key):
		a = self.collide.arrow(key)
		if a == 1:
			self.engine.position.moveleft()
		elif a == 2:
			self.engine.position.moveright()
		elif a == 3:
			self.engine.position.movedown()
		elif a == 4:
			self.engine.position.moveup()
	def choose(self,key):
		a = self.collide.value(key)
		if a != False:
			self.sel.value = a
		self.sel.change()
		if self.sel.value == 8:
			self.changer.mode = 2
		elif self.sel.value == 9 or self.sel.value == 10:
			self.changer.mode = 3
		else:
			self.changer.mode = 1
	def change(self,key):
		self.changer.checkall(key)